﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WyUnity.Data.Query;
using WyUnity.Data.TableMapper;

namespace SW_Interface.DTO
{
    public class BillDetailResponse : ViewBase<BillDetailResponse>
    {
        public char Result { get; set; }
        public string ErrMsg { get; set; }

        public List<BillEntry> Details { get; set; }

        [Map]
        public int BillID { get; set; }
        [Map]
        public string BillNo { get; set; }
        [Map]
        public string Remark { get; set; }
        [Map]
        public DateTime Date { get; set; }
        [Map]
        public string Biller { get; set; }
        [Map]
        public int Status { get; set; }
        [Map]
        public string SendStockNum { get; set; }
        [Map]
        public string RecieveStockNum { get; set; }
        [Map]
        public int BillType { get; set; }

        public static FieldBase pSendStockNum = new Field<BillDetailResponse>(p => p.SendStockNum);
        public static FieldBase pRecieveStockNum = new Field<BillDetailResponse>(p => p.RecieveStockNum);
        public static FieldBase pDate = new Field<BillDetailResponse>(p => p.Date);
        public static FieldBase pBillType = new Field<BillDetailResponse>(p => p.BillType);
        public static FieldBase pBillNo = new Field<BillDetailResponse>(p => p.BillNo);

        public override string GetSQLClip()
        {
            return @"
SELECT  isb.FBillNo BillNo ,isb.FInterID BillID,
        isb.FTranType BillType ,
        isb.FNote Remark ,
        isb.FDate [Date] ,
        tu.FName Biller ,
        isb.FStatus [Status] ,
        CASE WHEN isb.FTranType IN ( 21, 26, 28, 29, 43 ) THEN ts.FNumber
             WHEN isb.FTranType IN ( 24, 41 ) THEN ts2.FNumber
             ELSE ts2.FNumber
        END SendStockNum ,
        CASE WHEN isb.FTranType IN ( 1, 2, 5, 6, 40, 41 ) THEN ts.FNumber
             ELSE ''
        END RecieveStockNum
FROM    ICStockBill AS isb
        LEFT JOIN t_Stock AS ts ON isb.FDCStockID = ts.FItemID
        LEFT JOIN t_Stock AS ts2 ON isb.FSCStockID = ts.FItemID
        LEFT JOIN t_User AS tu ON isb.FBillerID = tu.FUserID
";
        }

        public static BillDetailResponse Get(string billno, int billtype)
        {
            try
            {
                if (billtype <= 0 || billtype > 45) throw new Exception("单据类型错误");
                if (string.IsNullOrEmpty(billno)) throw new Exception("单据编号必须提供");


                using (var db = DB.Open())
                {
                    var where = BillDetailResponse.pBillType == billtype & BillDetailResponse.pBillNo == billno;

                    var header = db.QuerySingle<BillDetailResponse>(where);
                    if (header == null) throw new Exception("单据不存在");

                    header.Details = db.QueryList<BillEntry>(BillEntry.pBillID == header.BillID);

                    header.Result = 'S';
                    return header;
                }

            }
            catch (Exception ex)
            {
                return new BillDetailResponse { Result = 'F', ErrMsg = "系统错误.\n" + ex.Message };
            }
        }
    }

    public class BillEntry : ViewBase<BillEntry>
    {

        [Map]
        public int BillID { get; set; }
        [Map]
        public int LineID { get; set; }
        [Map]
        public string ICItemNumber { get; set; }
        [Map]
        public string ICItemName { get; set; }
        [Map]
        public decimal Qty { get; set; }
        [Map]
        public string Unit { get; set; }
        [Map]
        public string SendStockNum { get; set; }
        [Map]
        public string RecieveStockNum { get; set; }

        public static FieldBase pBillID = new Field<BillEntry>(p => p.BillID);


        public override string GetSQLClip()
        {
            return @"
SELECT isbe.FInterID BillID,
isbe.FEntryID LineID ,
        tii.FNumber ICItemNumber ,
        tii.FName ICItemName ,
        isbe.FQty Qty ,
        tmu.FName Unit ,
        CASE WHEN isb.FTranType IN ( 21, 26, 28, 29, 43 ) THEN ts.FNumber
             WHEN isb.FTranType IN ( 24, 41 ) THEN ts2.FNumber
             ELSE ts2.FNumber
        END SendStockNum ,
        CASE WHEN isb.FTranType IN ( 1, 2, 5, 6, 40, 41 ) THEN ts.FNumber
             ELSE ''
        END RecieveStockNum
FROM    ICStockBill AS isb
        INNER JOIN ICStockBillEntry AS isbe ON isb.FInterID = isbe.FInterID
        LEFT JOIN t_ICItem AS tii ON isbe.FItemID = tii.FItemID
        LEFT JOIN t_MeasureUnit AS tmu ON tii.FUnitID = tii.FItemID
        LEFT JOIN t_Stock AS ts ON isb.FDCStockID = ts.FItemID
        LEFT JOIN t_Stock AS ts2 ON isb.FSCStockID = ts.FItemID";
        }
    }

}